package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApLikesBehaviorMapper;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.common.constants.message.HotArticleConstants;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.app.UpdateArticleMess;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadlocal.AppUserThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

@Service
public class ApLikesBehaviorServiceImpl extends ServiceImpl<ApLikesBehaviorMapper, ApLikesBehavior> implements ApLikesBehaviorService {

    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    KafkaTemplate<String,String> kafkaTemplate;

    /**
     * 添加点赞行为方法
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult like(LikesBehaviorDto dto) {
        //判断用户登录状态
        ApUser user = AppUserThreadLocalUtils.getUser();
        ApBehaviorEntry apBehaviorEntry;
        if (user != null) {
            //根据用户id查询用户行为实体
            apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), null);
        } else {
            apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(null, dto.getEquipmentId());
        }
        if(apBehaviorEntry==null){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"用户行为实体数据不存在");
        }
        //根据行为实体id和文章id查询是否点赞过
        ApLikesBehavior apLikesBehavior = getOne(Wrappers.<ApLikesBehavior>lambdaQuery()
                .eq(ApLikesBehavior::getArticleId, dto.getArticleId())
                .eq(ApLikesBehavior::getEntryId, apBehaviorEntry.getId()));

        if(apLikesBehavior!=null){
           //行为存在修改点赞状态
            apLikesBehavior.setOperation(dto.getOperation());
        }else {
            //不存在则创建点赞行为对象
            apLikesBehavior = new ApLikesBehavior();
            apLikesBehavior.setEntryId(apBehaviorEntry.getId());
            apLikesBehavior.setArticleId(dto.getArticleId());
            apLikesBehavior.setType((short)0);
            apLikesBehavior.setOperation(dto.getOperation());
            apLikesBehavior.setCreatedTime(new Date());
        }
        saveOrUpdate(apLikesBehavior);
        //点赞则发送kafka
        if(dto.getOperation()==0){
            UpdateArticleMess updateArticleMess = new UpdateArticleMess();
            updateArticleMess.setArticleId(dto.getArticleId());
            updateArticleMess.setType(UpdateArticleMess.UpdateArticleType.LIKES);
            kafkaTemplate.send(HotArticleConstants.HOTARTICLE_SCORE_INPUT_TOPIC,
                    UUID.randomUUID().toString(),JSON.toJSONString(updateArticleMess));
        }
        //返回响应
        return ResponseResult.okResult();
    }
}
